class Solution {
public:
    bool containsNearbyDuplicate(vector<int>& nums, int k) {
        unordered_map<int, int> mp;
        int len = nums.size();
        for (int i = 0; i < len; i++) {
            int x = nums[i];
            if (mp.count(x) && i - mp[x] <= k) return true;
            mp[x] = i;
        }
        return false;
    }
};
